﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace UCL.Powersource
{
    [DataContract(IsReference = true)]
    public class TimeFrameIteration : TimeFrame, ITimeFrameIteration
    {
        #region TfsIterationNode

        /// <summary>
        /// Field storage for property TfsIterationNode
        /// </summary>
        private NodeInfo _TfsIterationNode = default(NodeInfo);

        /// <summary>
        /// Property TfsIterationNode. TODODOC: Insert appropriate summary here
        /// </summary>
        public NodeInfo TfsIterationNode
        {
            get { return _TfsIterationNode; }
            set { if (_TfsIterationNode == value) return; _TfsIterationNode = value; TfsIterationNode_RecalculateDependencies(); }
        }

        private void TfsIterationNode_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => TfsIterationNode);
        }

        private void TfsIterationNode_Recalculate()
        {
            _TfsIterationNode = default(NodeInfo);
            //Insert your code here

            TfsIterationNode_RecalculateDependencies();
        }

        #endregion

        #region IterationPath

        /// <summary>
        /// Field storage for property IterationPath
        /// </summary>
        private string _IterationPath = default(string);

        /// <summary>
        /// Property IterationPath. TODODOC: Insert appropriate summary here
        /// </summary>
        [DataMember]
        public string IterationPath
        {
            get { return _IterationPath; }
            set { if (_IterationPath == value) return; _IterationPath = value; IterationPath_RecalculateDependencies(); }
        }

        private void IterationPath_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => IterationPath);
            //Label_RecalculateDependencies();
        }

        //private void IterationPath_Recalculate()
        //{
        //    _IterationPath = default(string);
        //    //Insert your code here

        //    IterationPath_RecalculateDependencies();
        //}

        #endregion


        #region NodePath

        /// <summary>
        /// Field storage for property NodePath
        /// </summary>
        [DataMember]
        private string _NodePath = default(string);

        /// <summary>
        /// Property NodePath. TODODOC: Insert appropriate summary here
        /// </summary>
        public string NodePath
        {
            get { return _NodePath; }
            set { if (_NodePath == value) return; _NodePath = value; NodePath_RecalculateDependencies(); }
        }

        private void NodePath_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => NodePath);
        }

        private void NodePath_Recalculate()
        {
            _NodePath = default(string);
            //Insert your code here

            NodePath_RecalculateDependencies();
        }

        #endregion



        #region Label

        /// <summary>
        /// Field storage for property Label
        /// </summary>
        [DataMember]
        private string _Label = default(string);

        /// <summary>
        /// Property Label. TODODOC: Insert appropriate summary here
        /// </summary>
        public override string Label
        {
            get { return string.IsNullOrWhiteSpace(_Label) ? IterationPath ?? "#NULL" : _Label; }
            set { if (_Label == value) return; _Label = value; Label_RecalculateDependencies(); }
        }

        private void Label_RecalculateDependencies()
        {
            //Recalculate all properties here

            //NotifyOfPropertyChange is usually placed as last statement - first change all dependencies and then fire property change
            NotifyOfPropertyChange(() => Label);
        }

        //private void Label_Recalculate()
        //{
        //    _Label = default(string);
        //    //Insert your code here

        //    Label_RecalculateDependencies();
        //}

        #endregion
	        
        public DateTime? StartDate
        {
            get;
            internal set;
        }

        public DateTime? FinishDate
        {
            get;
            internal set;
        }

        

        public string GetIterationPathForProject(ITfsTeamProjectConfiguration itpc)
        {
            return itpc.UniqueName + "\\" + IterationPath;
        }

        public void Merge(NodeInfo node)
        {
            if (node == null) throw new ArgumentNullException("node","TimeFrameIteration.SynchronizeTeamsAndTimeFrames cannot be called with null node info");
            var localcTFI = this;
            localcTFI.IterationPath =string.Empty;
            var iterationPattern="Iteration\\";
            var indexofIteration=node.Path.IndexOf(iterationPattern);
            if (indexofIteration>0) {
                localcTFI.IterationPath = node.Path.Substring(indexofIteration+iterationPattern.Length);
            }

            localcTFI.Label = localcTFI.IterationPath;
            localcTFI.NodePath = node.Path;

            if (String.IsNullOrWhiteSpace(IterationPath)) 
            {
                localcTFI.Label = "Project Latest";
                localcTFI.TimeFrameType = TimeFrameTypes.Project; 
            }
            var containsSprint = IterationPath.ToLower().Contains("sprint");
            var containsRelease = IterationPath.ToLower().Contains("release");
            if (containsRelease && containsSprint) localcTFI.TimeFrameType = TimeFrameTypes.Sprint;
            if (containsRelease && !containsSprint) localcTFI.TimeFrameType = TimeFrameTypes.Release;

            localcTFI.TfsIterationNode = node;
            localcTFI.StartDate = node.StartDate;
            localcTFI.FinishDate = node.FinishDate;
            localcTFI.SourceBranchPath = localcTFI.IterationPath.Replace("\\", "_"); 
            
        }

        public override string ToString()
        {
            return string.Format("{0} [{1}-{2}]", Label, StartDate, FinishDate);
        }
     
    }
}
